// 攻击弹道封装
library SimulateAttack requires InitFunction

    globals
        hashtable skht = InitHashtable()
    endglobals

    // 攻击弹道 (弹道需要攻击伤害，否则没有分裂)
    function timer_trajectory takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local integer tid = GetHandleId(t)
        local integer uid = LoadInteger(gameht, tid, 1)
        // 单位
        local unit u = LoadUnitHandle(gameht, tid, 9)
        local unit bu = LoadUnitHandle(gameht, tid, 10)
        local effect ect = LoadEffectHandle(gameht, tid, 3)
        // 移动次数
        // 要移动的距离
        local real jl = LoadReal(gameht, tid, 4) + 35
        local real shv = LoadReal(gameht, tid, 8)
    
        local real x0 = GetUnitX(u)
        local real y0 = GetUnitY(u)
        local real order_x = GetUnitX(bu)
        local real order_y = GetUnitY(bu)
        local real jd = getAngleBetweenXY(x0, y0, order_x, order_y)
        // 计算出马甲创建的位置
        local real x = CosBJ(jd) * (jl) + LoadReal(gameht, tid, 6)
        local real y = SinBJ(jd) * (jl) + LoadReal(gameht, tid, 7)
        // 总距离
        // local real totalJl = LoadReal(gameht, tid, 12)
        local real totalJl = getDistance(x0, y0, order_x, order_y)
        // 计算抛物线高度
        // -1600 表示400 的高度计算
        local real a = -400 / Pow(totalJl, 2.0)
        local real height = a * jl * (jl - totalJl)

        // // 设置弹道转向
        // call EXEffectMatRotateZ(ect, jd)
        // 设置弹道位置
        call EXSetEffectXY( ect, x, y )
        call EXSetEffectZ( ect, getZByXY(x, y) + height )        
    
        // 退出计时
        call SaveReal(gameht, tid, 4, jl)
        if(jl >= totalJl) then
            // 造成伤害 法术伤害
            call UnitDamageTarget( u, bu, shv, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS )

            call DestroyEffect(ect)
            call FlushChildHashtable(gameht, tid)
            call PauseTimer(t)
            call DestroyTimer(t)
        endif
    
        set u = null
        set bu = null
        set t = null
        set ect = null
    endfunction

    // 释放攻击弹道
    function releaseAttackBullet takes unit u, unit bu, string ddmdl, real shv, real height returns nothing
    
        local integer uid = GetPlayerId(GetOwningPlayer(u))
        local timer t
        local integer tid
        local effect ect
        local real x = GetUnitX(u)
        local real y = GetUnitY(u)
        local real order_x = GetUnitX(bu)
        local real order_y = GetUnitY(bu)
        local real jd = getAngleBetweenXY(x, y, order_x, order_y)

        set ect = AddSpecialEffect(ddmdl, x, y)
        call EXSetEffectZ( ect, getZByXY(x, y) + height )
        call EXEffectMatRotateZ(ect, jd)

        set t = CreateTimer()
        set tid = GetHandleId(t)
        call SaveInteger(gameht, tid, 1, uid)
        // call SaveReal(gameht, tid, 2, getAngleBetweenXY(x, y, order_x, order_y))
        // call SaveReal(gameht, tid, 12, getDistance(x, y, order_x, order_y))
        call SaveEffectHandle(gameht, tid, 3, ect)
        call SaveReal(gameht, tid, 6, x)
        call SaveReal(gameht, tid, 7, y)
        call SaveReal(gameht, tid, 8, shv)
        call SaveUnitHandle(gameht, tid, 9, u)
        call SaveUnitHandle(gameht, tid, 10, bu)

        call TimerStart(t, 0.02, true, function timer_trajectory)

        set u = null
        set bu = null
        set ddmdl = null
        set ect = null
        set t = null
    endfunction





    function releaseAttackBullet2 takes unit u, unit bu, string ddmdl, real shv, real height returns nothing
        
    endfunction
endlibrary


